package threadDemo;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Ajie
 * @date 2019-12-27
 * @function
 */
public class FixThreadPool {
    volatile static int index =0;
    public static void main(String[] args) {

        ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(3,new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("fixthread-pool-"+index);
                index++;
                return thread;
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();
        for (int i = 0; i < 3; i++) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("name "+Thread.currentThread().getName());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}
